<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>用户管理</title>
<style>
#pro_tb>div {
	margin-bottom: 5px;
}

#pro_tb>div>a, #department_tb>div>a {
	margin-bottom: 5px;
}
</style>
</head>
<body>
	<div class="content-container" data-options="region:'center'"
		style="float: left; width: 20%; border-right: 1px solid #ddd;">
		<div class="panel-header">
			<div class="panel-title">公司列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加公司</a>
				<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatepro()">编辑公司</a>
				<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletepro()">启用/禁用</a>
			</div>

		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog" style="width: 500px; padding: 10px 20px" closed="true"
		buttons="#pro-dlg-buttons">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">公司名：</td>
						<td>
							<input class="easyui-textbox" id="companyname" name="companyname"
								data-options="required:true,validType:'companynamevalidate'" style="width: 200px;" />
						</td>
					</tr>
					<!-- <tr>
						<td style="width: 100px;">免一级审核金额：</td>
						<td>
							<input class="easyui-textbox" id="level2money" name="level2money" data-options="validType:'number',prompt:'不超过此金额可跳过一级审批'"
								style="width: 200px;" />
						</td>
					</tr> -->
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton " onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">取消</a>
	</div>

	<div class="content-container" data-options="region:'center'"
		style="float: left; width: 20%; border-right: 1px solid #ddd;">
		<div class="panel-header">
			<div class="panel-title">部门列表</div>
		</div>
		<table id="department_table" class="easyui-datagrid" style="height: 100%">
		</table>
		<div id="department_tb">
			<div style="padding-bottom: 5px;">
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newdepartment()">添加部门</a>
				<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatedepartment()">编辑部门</a>
				<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletedepartment()">启用/禁用</a>
			</div>

		</div>
	</div>

	<div id="departmentDlg" class="easyui-dialog info_dialog" style="width: 500px; padding: 10px 20px"
		closed="true" buttons="#department-dlg-buttons">
		<div class="title"></div>
		<form id="departmentFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">部门名：</td>
						<td>
							<input class="easyui-textbox" id="departmentname" name="departmentname" data-options="required:true"
								style="width: 200px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="department-dlg-buttons">
		<a href="# " class="easyui-linkbutton " onclick="savedepartment()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#departmentDlg').dialog('close')">取消</a>
	</div>

	<div class="content-container" data-options="region:'center'" style="float: left; width: 60%;">
		<div class="panel-header">
			<div class="panel-title">用户列表</div>
		</div>
		<table id="child_table" class="easyui-datagrid" style="height: 100%">
		</table>
	</div>
	<div id="childDlg" class="easyui-dialog info_dialog" style="width: 40%; padding: 10px 20px" closed="true"
		buttons="#child-dlg-buttons">
		<div class="title"></div>
		<form id="childFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">用户名：</td>
						<td>
							<input type="text" class="easyui-textbox" id="username" name="username"
								data-options="required:true,validType:'usernamevalidate'" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td></td>
						<td>
							<input class="easyui-checkbox" type="checkbox" id="password" name="password" />
							&nbsp;更新密码
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">姓名：</td>
						<td>
							<input type="text" class="easyui-textbox" id="name" name="name" required="true" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">工号：</td>
						<td>
							<input type="text" class="easyui-textbox" id="jobno" name="jobno" required="true" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">身份证号：</td>
						<td>
							<input type="text" class="easyui-textbox" id="idcard" name="idcard" required="true"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">手机号：</td>
						<td>
							<input type="text" class="easyui-textbox" id="mobile" name="mobile"
								data-options="validType:['mobilevalidate','phoneNum']" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">级别：</td>
						<td>
							<input style="width: 200px;" id="role" name="role" class="easyui-combobox"
								data-options=" editable : false,
							    valueField: 'id',
							    textField: 'name',
							    required:true,
							    url: 'roles',
							    panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">角色：</td>
						<td>
							<input style="width: 200px;" id="level" name="level" class="easyui-combobox"
								data-options="editable : false,
							    valueField: 'id',
							    textField: 'name',
							    required:true,
							    panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">性别：</td>
						<td>
							<select class="easyui-combobox" id="sex" data-options="	editable : false,required:true,panelHeight: 'auto'" name="sex"
								style="width: 80px;">
								<option value="男">男</option>
								<option value="女">女</option>
							</select>
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="child-dlg-buttons">
		<a href="# " class="easyui-linkbutton " onclick="saveChild()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#childDlg').dialog('close')">取消</a>
	</div>
	<div id="child_tb">
		<div>
			<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newchild()">添加用户</a>
			<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatechild()">更新用户</a>
			<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletechild()">启用/禁用</a>
			<input id="excel" name="excel" class="easyui-filebox search-input" style="width: 200px;"
				accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
				data-options="prompt:'请选择文件...',buttonText:'选择excel'">
			<a id="booten" href="javascript:void(0)" class="easyui-linkbutton add" onclick="uploadExcel()"
				style="width: 80px;">导入</a>
		</div>

	</div>

</body>

<script type="text/javascript">
	var currentPro;
	var currentDepartment;
	var currentChild;
	$(document).ready(function() {
		$('#pro_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#pro_tb',
			url : "company",
			method : 'get',
			onSelect : function(index,row) {
				currentPro = row;
				/* if (row.status == 3){
					$("#department_tb").find(".easyui-linkbutton").linkbutton('disable');
				}else{
					$("#department_tb").find(".easyui-linkbutton").linkbutton('enable');
				} */
				refreshDepartmentTable();
			},
			columns : [ [ {
				field : 'name',
				title : '公司名',
				width : ''
			},{
				field : 'status',
				title : '状态',
				width : '',
				formatter : function(value,row,index) {
					if (value == 2){
						return value = '启用';
					}else if (value == 3){
						return value = '禁用';
					}
				}
			} ] ]
		});
		$('#department_table').datagrid({
			toolbar : '#department_tb',
		});
		$('#child_table').datagrid({
			toolbar : '#child_tb'
		});
	});
	function refreshDepartmentTable() {
		$('#department_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#department_tb',
			url : "department?companyid=" + currentPro.id,
			method : 'get',
			onSelect : function(index,row) {
				currentDepartment = row;
				refreshChildTable();
			},
			onDblClickRow : function(index,row) {
				currentDepartment = row;
				updatedepartment();
			},
			columns : [ [ {
				field : 'name',
				title : '部门名',
				width : ''
			},{
				field : 'status',
				title : '状态',
				width : '',
				formatter : function(value,row,index) {
					if (value == 2){
						return value = '启用';
					}else if (value == 3){
						return value = '禁用';
					}
				}
			} ] ]
		});
	}
	function refreshChildTable() {
		$('#child_table').datagrid({
			rownumbers : true,
			singleSelect : true,
			pagination : true,
			fitColumns : false,
			border : false,
			striped : true,
			toolbar : '#child_tb',
			url : "user?departmentid=" + currentDepartment.id,
			method : 'get',
			onSelect : function(index,row) {
				currentChild = row;
			},
			onDblClickRow : function(index,row) {
				currentChild = row;
				updatechild();
			},
			columns : [ [ {
				field : 'username',
				title : '用户名',
				width : ''
			},{
				field : 'name',
				title : '姓名',
				width : ''
			},{
				field : 'jobno',
				title : '工号',
				width : ''
			},{
				field : 'idcard',
				title : '身份证号',
				width : ''
			},{
				field : 'mobile',
				title : '手机号',
				width : ''
			},{
				field : 'roleid',
				title : '级别',
				width : '',
				formatter : function(value,row,index) {
					if (row.role != null){
						return row.role.name;
					}else{
						return '';
					}
				}
			},{
				field : 'levelid',
				title : '角色',
				width : '',
				formatter : function(value,row,index) {
					if (row.level != null){
						return row.level.name;
					}else{
						return '';
					}
				}
			},{
				field : 'sex',
				title : '性别',
				width : ''
			},{
				field : 'status',
				title : '状态',
				width : '',
				formatter : function(value,row,index) {
					if (value == 2){
						return value = '启用';
					}else if (value == 3){
						return value = '禁用';
					}
				}
			} ] ]
		});
	}
	function refreshProTable() {
		$('#pro_table').datagrid('reload');
	}
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加公司');
		$("#proFm").form('clear');
		currentPro = {};
	}
	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$("#proDlg").dialog('open').dialog('setTitle', '更新公司');
			$("#proFm").form('load', {
				companyname : currentPro.name
			});
		}else{
			$.messager.alert('更新', '请选择要更新的公司', 'warning');
		}
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		currentPro.name = $("#companyname").textbox('getValue');
		$.ajax({
			type : 'POST',
			url : "companySave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存公司',
						msg : '保存公司成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				}else{
					$.messager.alert('保存公司', '保存公司失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function deletepro() {
		var row = $("#pro_table").datagrid('getSelected');
		if (row){
			var status;
			if (row.status == 2){
				status = 3;
			}else{
				status = 2
			}
			$.ajax({
				type : 'POST',
				url : "companyDelete.do",
				data : {
					companyid : row.id,
					status : status
				},
				success : function(data) {
					if (data.success){
						$.messager.show({
							title : '操作',
							msg : '操作成功',
							timeout : 5000,
							showType : 'slide'
						});
						refreshProTable();
						/* if (status == 3){
							$("#child_tb").find(".easyui-linkbutton").linkbutton('disable');
						}else{
							$("#child_tb").find(".easyui-linkbutton").linkbutton('enable');
						} */
					}else{
						$.messager.alert('操作', '操作失败', 'error');
					}
				},
				dataType : "json"
			});
		}else{
			$.messager.alert('操作', '请选择要启用/禁用的公司', 'warning');
		}
	}
	function newdepartment() {
		$("#departmentDlg").dialog('open').dialog('setTitle', '添加部门');
		$("#departmentFm").form('clear');
		currentDepartment = {};
	}
	function updatedepartment() {
		currentDepartment = $("#department_table").datagrid('getSelected');
		if (currentDepartment){
			$("#departmentDlg").dialog('open').dialog('setTitle', '更新部门');
			$("#departmentFm").form('load', {
				departmentname : currentDepartment.name
			});
		}else{
			$.messager.alert('更新', '请选择要更新的部门', 'warning');
		}
	}
	function savedepartment() {
		if (!$("#departmentFm").form('validate'))
			return;
		currentDepartment.name = $("#departmentname").textbox('getValue');
		currentDepartment.companyid = currentPro.id;
		$.ajax({
			type : 'POST',
			url : "departmentSave.do",
			data : JSON.stringify(currentDepartment),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存部门',
						msg : '保存部门成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshDepartmentTable();
				}else{
					$.messager.alert('保存部门', '保存部门失败', 'error');
				}
				$('#departmentDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function deletedepartment() {
		var row = $("#department_table").datagrid('getSelected');
		if (row){
			var status;
			if (row.status == 2){
				status = 3;
			}else{
				status = 2
			}
			$.ajax({
				type : 'POST',
				url : "departmentDelete.do",
				data : {
					departmentid : row.id,
					status : status
				},
				success : function(data) {
					if (data.success){
						$.messager.show({
							title : '操作',
							msg : '操作成功',
							timeout : 5000,
							showType : 'slide'
						});
						refreshDepartmentTable();
						/* 	if (status == 3){
								$("#department_tb").find(".easyui-linkbutton").linkbutton('disable');
							}else{
								$("#department_tb").find(".easyui-linkbutton").linkbutton('enable');
							} */
					}else{
						$.messager.alert('操作', '操作失败', 'error');
					}
				},
				dataType : "json"
			});
		}else{
			$.messager.alert('操作', '请选择要启用/禁用的部门', 'warning');
		}
	}
	function newchild() {
		if (currentDepartment){
			$("#childDlg").dialog('open').dialog('setTitle', '添加用户');
			$("#childFm").form('clear');
			currentChild = {};
			$("#childDlg").dialog('open').dialog('setTitle', '更新用户');
			var url = "levellist?companyid="+currentPro.id;
			$('#level').combobox('reload', url);
			$("#password").checkbox('check');
			$("#password").checkbox('disable');
		}else{
			$.messager.alert('更新', '请选择部门', 'warning');
		}
	}
	function updatechild() {
		currentChild = $("#child_table").datagrid('getSelected');
		if (currentChild){
			$("#childDlg").dialog('open').dialog('setTitle', '更新用户');
			var url = "levellist?companyid="+currentPro.id;
			$('#level').combobox('reload', url);
			$("#childFm").form('load', {
				username : currentChild.username,
				name : currentChild.name,
				jobno : currentChild.jobno,
				idcard : currentChild.idcard,
				sex : currentChild.sex,
				mobile : currentChild.mobile
			});
			$("#role").combobox('setValue', currentChild.role.id);
			$("#level").combobox('setValue', currentChild.level.id);
			$("#password").checkbox('uncheck');
			$("#password").checkbox('enable');
		}else{
			$.messager.alert('更新', '请选择要更新的用户', 'warning');
		}
	}
	function saveChild() {
		if (!$("#childFm").form('validate'))
			return;
		currentChild.username = $("#username").textbox('getValue');
		currentChild.name = $("#name").textbox('getValue');
		currentChild.jobno = $("#jobno").textbox('getValue');
		currentChild.idcard = $("#idcard").textbox('getValue');
		currentChild.companyid = currentPro.id;
		currentChild.departmentid = currentDepartment.id;
		currentChild.roleid = $("#role").combobox('getValue');
		currentChild.levelid = $("#level").combobox('getValue');
		currentChild.sex = $("#sex").combobox('getValue');
		currentChild.mobile = $("#mobile").textbox('getValue');
		if ($('.checkbox-checked').length > 0){
			currentChild.setpwd = true;
		}else{
			currentChild.setpwd = false;
		}
		$.ajax({
			type : 'POST',
			url : "userSave.do",
			data : JSON.stringify(currentChild),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存用户',
						msg : '保存用户成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshChildTable();
				}else{
					$.messager.alert('保存用户', '保存用户失败', 'error');
				}
				$('#childDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function deletechild() {
		var row = $("#child_table").datagrid('getSelected');
		if (row){
			var status;
			if (row.status == 2){
				status = 3;
			}else{
				status = 2
			}
			$.ajax({
				type : 'POST',
				url : "userDelete.do",
				data : {
					userid : row.id,
					status : status
				},
				success : function(data) {
					if (data.success){
						$.messager.show({
							title : '操作',
							msg : '操作成功',
							timeout : 5000,
							showType : 'slide'
						});
						refreshChildTable();
					}else{
						$.messager.alert('操作', '操作失败', 'error');
					}
				},
				dataType : "json"
			});
		}else{
			$.messager.alert('操作', '请选择要启用/禁用的用户', 'warning');
		}
	}
	

	function uploadExcel() {
		var excel = document.getElementById("filebox_file_id_1").files[0];
		//对文件格式进行校验  
		var fileName = excel.name;
		var d1 = /\.[^\.]+$/.exec(fileName);
		if (fileName == ""){
			$.messager.alert('Excel批量用户导入', '请选择将要上传的文件!');
			return false;
		}else if (d1 != ".xls" && d1 != ".xlsx"){
			$.messager.alert('提示', '请选择excel文件！', 'info');
			return false;
		}
		$("#booten").linkbutton('disable');
		var formData = new FormData();
		formData.append("excel", excel);
		$.ajax({
			type : 'POST',
			url : "importExcel.do",
			cache : false,
			data : formData,
			processData : false,
			contentType : false,
			success : function(data) {
				if (data.success){
					$.messager.alert('提示!', '导入成功', 'info', function() {
						$("#booten").linkbutton('enable');
						$('#importExcel').dialog('close');
					});
					refreshChildTable();
				}else{
					$.messager.confirm('提示', "导入失败!");
					$("#booten").linkbutton('enable');
				}
			},
			dataType : "json"
		});
	}
	
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		phoneNum : { //验证手机号   
			validator : function(value, param) {
				return /^1[3-9]+\d{9}$/.test(value);
			},
			message : '请输入正确的手机号码。'
		},  
		usernamevalidate : {
			validator : function(value,param) {
				var userinfo = {};
				userinfo.username = value.trim();
				if (currentChild.id){
					userinfo.id = currentChild.id;
				}else{
					userinfo.id = -1;
				}
				var res = false;
				$.ajax({
					type : 'POST',
					async : false,
					url : 'check/username',
					data : userinfo,
					success : function(data) {
						res = data.success;
					},
					dataType : "json"
				});
				return res;
			},
			message : '用户名已存在'
		},
		companynamevalidate : {
			validator : function(value,param) {
				var company = {};
				company.name = value.trim();
				if (currentPro.id){
					company.id = currentPro.id;
				}else{
					company.id = -1;
				}
				var res = false;
				$.ajax({
					type : 'POST',
					async : false,
					url : 'check/companyname',
					data : company,
					success : function(data) {
						res = data.success;
					},
					dataType : "json"
				});
				return res;
			},
			message : '公司名已存在'
		},
		mobilevalidate : {
			validator : function(value,param) {
				var userinfo = {};
				userinfo.mobile = value.trim();
				if (currentChild.id){
					userinfo.id = currentChild.id;
				}else{
					userinfo.id = -1;
				}
				var res = false;
				$.ajax({
					type : 'POST',
					async : false,
					url : 'check/mobile',
					data : userinfo,
					success : function(data) {
						res = data.success;
					},
					dataType : "json"
				});
				return res;
			},
			message : '手机号已注册'
		}
	});
</script>

</html>